package com.qidian.hw.dao.impl;

import java.sql.*;

/**
 * @Author ꒰ঌ(李ᆺ琛)໒꒱
 * @Date 2022/8/8 16:50
 * @Version 1.8
 */
public class BaseDAO{
    private static final String URL = "jdbc:mysql://localhost:3306/hengwang?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8";
    private static final String USER = "root";
    private static final String PASSWORD = "root";
    private static final String DRIVER_NAME = "com.mysql.cj.jdbc.Driver";

    //获取指定url、用户名和密码的数据库连接
    public Connection getCon() {
        Connection con = null;
        try {
            Class.forName (DRIVER_NAME);
            con = DriverManager.getConnection (URL, USER, PASSWORD);
        } catch (Exception e) {
            e.printStackTrace ();
        }
        return con;
    }

    //关闭全部连接
    public void closeAll(ResultSet rs, Statement stmt, Connection con) {
        try {
            if (rs != null)
                rs.close ();
            if (stmt != null)
                stmt.close ();
            if (con != null)
                con.close ();
        } catch (SQLException throwable) {
            throwable.printStackTrace ();
        }
    }
}
